<?php

class lfView extends lfCollection
{

	/**
	 * 载入模板片段
	 *
	 * @param string $partialName 模板名
	 */
	public function __construct()
	{
		$app = lfApplication::GetInstance();
		$appName = $app->APP_NAME();
		$this->moduleName = $app->MODULE_NAME();
		$this->viewPath = PATH_ROOT . '/applications/' . $appName . '/views/';
		$this->layoutPath = $this->viewPath . "layouts/";
		$this->templatePath = $this->viewPath . "templates/";
		$this->baseUrl = BASE_URL;
	}

	public function GetPartialContent($partialName)
	{
		$filename1 = $this->templatePath . $partialName . '.phtml';
		$filename2 = $this->templatePath . $this->moduleName . "/$partialName.phtml";
		$filename = is_file($filename1) ? $filename1 : $filename2;
		
		if (is_file($filename))
		{
			ob_start();
			include $filename;
			$contents = ob_get_contents();
			ob_end_clean();
			return $contents;
		}
		return false;
	}

	public function Display()
	{
		$file = $this->layoutPath . $this->layoutName . '.phtml';
		
		if (file_exists($file))
		{
			include $file;
		}
		
		return false;
	}

	public function SetTemplate($templateName = "index")
	{
		$this->templateName = $templateName;
	}

	public function SetLayout($layoutName = "")
	{
		$this->layoutName = $layoutName;
	}

	public function VIEW_PATH()
	{
		return $this->viewPath;
	}

	public function __set($name, $value)
	{
		$this[$name] = $value;
	}

	public function __get($name)
	{
		return parent::offsetGet($name);
	}

	public function offsetGet($offset)
	{
		echo $this->data[$offset];
		return $this->data[$offset];
	}

	public function AddStyles($arStyles)
	{
		$strBegin = '<link type="text/css" rel="stylesheet" rev="stylesheet" href="';
		$strEnd = '" />' . "\n";
		$strStyles = "";
		foreach ( $arStyles as $k => $v )
		{
			$strStyles .= $strBegin . 'styles/' . $v . '.css' . $strEnd;
		}
		$this->styles = $strStyles;
	}

	public function AddScripts($arScripts)
	{
		$strBegin = '<script type="text/javascript" src="';
		$strEnd = '"></script>' . "\n";
		$strScripts = "";
		foreach ( $arScripts as $k => $v )
		{
			$strScripts .= $strBegin . BASE_URL . 'scripts/' . $v . '.js' . $strEnd;
		}
		$this->scripts = $strScripts;
	}

	protected function BaseUrl()
	{
		return $this->baseUrl;
	}

	protected function MakeUrl($moduleName = "", $controllerName = "", $methodName = "", $argmentsList = "", $queryString = "")
	{
		//		echo $moduleName;
		$url = "";
		$url = ! empty($moduleName) ? $moduleName . '/' : '';
		//		echo $url;
		$url = ! empty($controllerName) ? $moduleName . '-' . $controllerName . '/' : $moduleName . '/';
		$url .= ! empty($methodName) ? $methodName . '/' : '';
		$url .= ! empty($argmentsList) ? str_replace(',', '-', $argmentsList) . '.html' : '';
		//		str_replace()
		$url .= ! empty($queryString) ? '?' . $queryString : '';
		return $this->baseUrl . $url;
	}

	protected function RenderStyles()
	{
		echo $this->styles;
	}

	protected function RenderScripts()
	{
		echo $this->scripts;
	}

	protected function Partial($partialName)
	{
		$file = $this->templatePath . $partialName . '.phtml';
		
		if (file_exists($file))
		{
			include $file;
		}
		
		return false;
	}

	protected function RenderContent()
	{
		$this->Partial($this->moduleName . '/' . $this->templateName);
	}

	/**
	 * 返回HTML指定ID的HtmlElementCtrl对象
	 *
	 * @param string $id
	 * @return lfJquery
	 */
	function HtmlElement($selectString)
	{
		if (! is_null($this->jquery))
		{
			$this->jquery->InitSelector($selectString);
			return $this->jquery;
		}
		return FALSE;
	}

	function SetJquery(lfJquery $jquery)
	{
		$this->jquery = $jquery;
	}
	
	/**
	 * jquery
	 *
	 * @var lfJquery
	 */
	protected $jquery = NULL;
	
	private $baseUrl = "";
	private $viewPath = ""; // 视图路径
	private $layoutPath = ""; // 布局路径
	private $templatePath = ""; // 模板路径
	

	private $moduleName = "";
	private $layoutName = "";
	private $templateName = "";
	
	private $styles = "";
	private $scripts = "";
}

?>